package net.sunofbeach.blog.controller.admin;


import net.sunofbeach.blog.interceptor.CheckTooFrequentCommit;
import net.sunofbeach.blog.pojo.NavigationCategory;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.INavigationCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 管理中心，导航分类的API
 */
@RestController
@RequestMapping("/admin/navigation_category")
public class NavigationCategoryAdminApi {

    @Autowired
    private INavigationCategoryService navigationCategoryService;

    /**
     * 添加分类
     * 需要管理员权限
     */
    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @PostMapping
    public ResponseResult addNavigationCategory(@RequestBody NavigationCategory navigationCategory){
        return navigationCategoryService.addNavigationCategory(navigationCategory);
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{navigation_categoryId}")
    public ResponseResult deleteNavigationCategory(@PathVariable("navigation_categoryId") String navigationCategoryId){
        return navigationCategoryService.deleteNavigationCategory(navigationCategoryId);
    }

    /**
     * 更新分类
     */
    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @PutMapping("/{navigation_categoryId}")
    public ResponseResult updateNavigationCategory(@PathVariable("navigation_categoryId") String navigationCategoryId,@RequestBody NavigationCategory navigationCategory){
        return navigationCategoryService.updateNavigationCategory(navigationCategoryId,navigationCategory);
    }

    /**
     * 获取分类
     *
     * 什么时候会用到这个？修改的时候，获取一下，填充弹窗
     * 不获取也行，从列表获取数据
     *
     * 权限：管理员权限
     */
    @GetMapping("/{navigation_categoryId}")
    public ResponseResult getNavigationCategory(@PathVariable("navigation_categoryId") String navigationCategoryId){
        return navigationCategoryService.getNavigationCategory(navigationCategoryId);
    }

    /**
     * 获取分类列表
     *
     *  权限：管理员权限
     */
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list")
    public ResponseResult listNavigationCategories(){
        return navigationCategoryService.listNavigationCategories();
    }

}
